package com.dongnam.quanly.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PropertiesUtil {

	/**
	 * support properties value which contains Java System Property, e.g.
	 * ${user.home}
	 * 
	 * @param propValue
	 * @return
	 */
	public static String parsePropertiesValue(String propValue) {
		if (propValue == null)
			return propValue;

		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("\\$\\{(.+)\\}");
		Matcher matcher = pattern.matcher(propValue);
		while (matcher.find()) {
			String key = matcher.group(1);
			String val = System.getProperty(key);
			if (val != null) {
				matcher.appendReplacement(sb, Matcher.quoteReplacement(val));
			}
		}
		matcher.appendTail(sb);

		return sb.toString();
	}

}
